## Table 1: Covid-19 Effects on Asian Hate Crimes

rm(list = ls())

setwd('path/to/replication/')

library(data.table)
library(estimatr)
library(texreg)

load('./data/panel_month_dummies.RData')

## Panel A: Health Threat
out_death_jan_simple <- lm_robust(hc_pc_asians ~ covid2*excess_deaths_jan_d, data=panel, se_type='stata',
                                  clusters = panelvar)

if (!file.exists('./output/out_death_jan.RData') &
    !file.exists('./output/out_death_jan_con.RData') &
    !file.exists('./output/out_death_jan_con_trend.RData') &
    !file.exists('./output/out_death_jan_con_trend_all.RData')) {
  source('./code/table1areg.R')
}

out_death_jan <- get(load('./output/out_death_jan.RData'))
out_death_jan_con <- get(load('./output/out_death_jan_con.RData'))
out_death_jan_con_trend <- get(load('./output/out_death_jan_con_trend.RData'))
out_death_jan_con_trend_all <- get(load('./output/out_death_jan_con_trend_all.RData'))

# to produce table row 'Average Hate Crimes' in Panel A
load('./output/effective_sample_data_death_jan.RData')
means <- unlist(c(round(effective_sample_death[1,2], 5),
                  round(effective_sample_death_fe[1,2], 5),
                  round(effective_sample_death_fe_con[1,2], 5),
                  round(effective_sample_death_fe_con_trend[1,2], 5),
                  round(effective_sample_death_fe_con_trend_prov[1,2], 5)))

panela <- texreg(list(out_death_jan_simple, out_death_jan, out_death_jan_con, out_death_jan_con_trend, out_death_jan_con_trend_all),
               include.ci = FALSE,
               include.adjrs = FALSE, include.rmse = FALSE,
               stars = c(0.001, 0.01, 0.05), digits = 5,
               custom.coef.map = list('excess_deaths_jan_d' = 'Excess deaths', 'covid2'= 'After Jan, 2020', 
                                      'covid2:excess_deaths_jan_d' = 'After Jan, 2020 x Excess deaths',
                                      'covid_death' = 'After Jan, 2020 x Excess deaths'),
               custom.gof.rows = list("Average Hate Crimes" = as.vector(means)),
               symbol = "\\circ",
               caption = "Panel A: Health Threat",
               caption.above = TRUE)

write(panela, file.path('./table1_panela.tex'))

## Panel B: Economic Threat
out_econ_simple <- lm_robust(hc_pc_asians ~ covid2*losing_income_d, data=panel, se_type='stata',
                             clusters = panelvar)

 
if (!file.exists('./output/out_econ.RData') &
    !file.exists('./output/out_econ_con.RData') &
    !file.exists('./output/out_econ_con_trend.RData') &
    !file.exists('./output/out_econ_con_trend_all.RData')) {
  source('./code/table1breg.R')
}

out_econ <- get(load('./output/out_econ.RData'))
out_econ_con <- get(load('./output/out_econ_con.RData'))
out_econ_con_trend <- get(load('./output/out_econ_con_trend.RData'))
out_econ_con_trend_all <- get(load('./output/out_econ_con_trend_all.RData'))

# to produce table row 'Average Hate Crimes' in Panel B
load('./output/effective_sample_data.RData')
means <- unlist(c(round(effective_sample_econ[1,2], 5),
         round(effective_sample_econ_fe[1,2], 5),
         round(effective_sample_econ_fe_con[1,2], 5),
         round(effective_sample_econ_fe_con_trend[1,2], 5),
         round(effective_sample_econ_fe_con_trend_prov[1,2], 5)))

panelb <- texreg(list(out_econ_simple, out_econ, out_econ_con, out_econ_con_trend, out_econ_con_trend_all),
       include.ci = FALSE,
       include.adjrs = FALSE, include.rmse = FALSE,
       stars = c(0.001, 0.01, 0.05, 0.1), digits = 5,
       custom.coef.map = list('losing_income_d' = 'Expected unemployment', 'covid2'= 'After Jan, 2020', 
                                      'covid2:losing_income_d' = 'After Jan, 2020 x Expected unemployment',
                              'covid_econ' = 'After Jan, 2020 x Expected unemployment'),
       custom.gof.rows = list("Average Hate Crimes" = as.vector(means)),
       symbol = "\\circ",
       caption = "Panel B: Economic Threat",
       caption.above = TRUE)

write(panelb, file.path('./table1_panelb.tex'))
